﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace translator
{
    public class LoadOrNewStringTableCommand : ICommand
    {
        MainWindow mainWindow;
        public LoadOrNewStringTableCommand(MainWindow mainWindow)
        {
            this.mainWindow = mainWindow;
        }

        public event EventHandler CanExecuteChanged;

        public bool CanExecute(object parameter)
        {
            return true;
        }

        public void Execute(object parameter)
        {
            
            LoadOrNewStringTable();
        }

        public void LoadOrNewStringTable()
        {
            //  还没有实现新建String表

            mainWindow.Log("Try to load or new String table...");
            mainWindow.stringEditWindow.ToHideWindow();
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.InitialDirectory = System.Environment.CurrentDirectory;      //Application.StartupPath 
            //openFileDialog.FileName = "翻译中间文件 (*.db)|*.db|All files (*.*)|*.*";
            openFileDialog.FilterIndex = 2;
            openFileDialog.RestoreDirectory = false;
            if (openFileDialog.ShowDialog() == true)
            {
                mainWindow.Log("Selected file path: " + openFileDialog.FileName + " ...");
                if (!File.Exists(openFileDialog.FileName)) { mainWindow.Log("File Not Exists!"); return; }
                if (!mainWindow.OpenDatabase(openFileDialog.FileName)) { mainWindow.Log("End Load Process."); return; }
                mainWindow.ReadDataBase();
                mainWindow.GetHintTable();
                mainWindow.StringHeader.Header = "String[" + mainWindow.Translated + "/" + mainWindow.model.StringLst.Count + "]";     //UI刷新,写在这里不妥当
                mainWindow.Datagrid.DataContext = mainWindow.model;
                mainWindow.Log("Load String table: " + openFileDialog.FileName + " Successed!");
                ChangeTitle(openFileDialog.FileName);

                return;
            }
            
            mainWindow.Log("Load cancelled.");
        }
        public void ChangeTitle(string filename)
        {
            mainWindow.Title = "Twine引擎游戏翻译器  " + filename;
        }
    }
}
